package com.tanhua.server.service;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.tanhua.common.mapper.QuestionMapper;
import com.tanhua.common.pojo.Question;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 陌聊问题 业务层
 */
@Service
public class QuestionService {
    @Autowired
    private QuestionMapper questionMapper;

    /**
     * 查询陌聊问题
     *
     * @param userId 用户id
     * @return 陌聊问题对象
     */
    public Question queryQuestion(Long userId) {
        QueryWrapper<Question> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        return this.questionMapper.selectOne(queryWrapper);

    }

    /**
     * 修改陌生人问题
     *
     * @param userId  用户id
     * @param content 问题
     * @return
     */
    public Boolean updateQuestions(Long userId, String content) {
        // 先查看用户是否已创建陌生人问题
        QueryWrapper<Question> queryCount = new QueryWrapper<>();
        queryCount.eq("user_id", userId);
        Integer count = this.questionMapper.selectCount(queryCount);
        if (count == 0) {
            /* 添加陌生人问题 */
            // 用户没有创建陌生人问题
            Question question = new Question();
            question.setUserId(userId);
            question.setTxt(content);

            return this.questionMapper.insert(question) == 1;
        } else {
            /* 修改陌生人问题 */
            UpdateWrapper<Question> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("user_id", userId);
            updateWrapper.set("txt", content);

            return this.questionMapper.update(null, updateWrapper) == 1;
        }

    }
}
